
export const dateEqual = (a: Date, b: Date) => {
    if ([a instanceof Date, b instanceof Date].includes(false)) return false
    return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()
}

export const dateString = (date: Date) => {
    if (!(date instanceof Date)) return date || '';
    return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`
}

export const getDate = (str: Date | string) => {
    if (str instanceof Date) {
        return str;
    }
    if (typeof str !== 'string') {
        return null;
    }
    const [year, month, day] = str.split(/[/\-]/);
    return new Date(+year, +month, +day);
}

export const getMonthSpan = (date = new Date(), dateSplit = '-') => {
    const ey = date.getFullYear();
    const em = date.getMonth();
    const ed = date.getDate();
    const end = [ey, em + 1, ed].join(dateSplit);
    date.setMonth(em, 0);
    const sy = date.getFullYear();
    const sm = date.getMonth();
    let sd = date.getDate();
    if (sd > ed) {
        sd = ed;
    }
    const start = [sy, sm + 1, sd].join(dateSplit);
    return [start, end];
};